<?php
namespace Model;

class LoginHandler {

	private $database = NULL;
	private $loginSession;
	private $userName = 'username';
	private $passWord = 'password';

	public function __construct($a_database) {
		$this->database = $a_database;
	}
	//funktion som kollar om man är inloggad
	public function IsLoggedIn() {
		if (isset($_SESSION[$this->userName]) && isset($_SESSION[$this->passWord])) {
			return true;
		}
		return false;
		
	}
	//Funktion som loggar in dig
	public function DoLogin($username, $password){
		//$passworda = md5($password);
		if(isset($username) && isset ($password)) {
			if($stmt = $this->database->SqlStatement('SELECT User_Id, UserName, PassWord FROM users WHERE UserName = ?')) {
				$stmt->bind_param('s', $username);
				$stmt->execute();
				$stmt->bind_result($u_id, $dbUsername, $dbPassword);

				while ($stmt->fetch()) {
					if ($password == $dbPassword) {
						$_SESSION[$this->userName] = $dbUsername;
						$_SESSION[$this->passWord] = $dbPassword;
						setcookie('userID', $u_id, time() + 3600 * 24 * 7);
						return true;
					}
				}
				$stmt->close();
			}	
		}
	}
	//Funktion som loggar ut dig
	public function DoLogout(){ 
		if (isset($_SESSION[$this->userName])) {
			unset($_SESSION[$this->userName]);
			unset($_SESSION[$this->passWord]);
			unset($_COOKIE['userID']);
		}
	}
}
